package com.eduhzy.ttw.clazz.di.module;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.clazz.R;
import com.eduhzy.ttw.clazz.mvp.contract.EditReviewContract;
import com.eduhzy.ttw.clazz.mvp.model.EditReviewModel;
import com.eduhzy.ttw.clazz.mvp.model.entity.ClazzScoreData;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.annotation.ScopeType;
import com.eduhzy.ttw.commonsdk.decoration.GridSpacingItemDecoration;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.di.scope.ActivityScope;

import java.util.ArrayList;
import java.util.List;

import dagger.Module;
import dagger.Provides;

/**
 * @author xubo
 * @date 2018-12-5 10:21:24
 */
@Module
public class EditReviewModule {
    private EditReviewContract.View view;

    /**
     * 构建EditReviewModule时,将View的实现类传进来,这样就可以提供View的实现类给presenter
     *
     * @param view
     */
    public EditReviewModule(EditReviewContract.View view) {
        this.view = view;
    }

    @ActivityScope
    @Provides
    EditReviewContract.View provideEditReviewView() {
        return this.view;
    }

    @ActivityScope
    @Provides
    EditReviewContract.Model provideEditReviewModel(EditReviewModel model) {
        return model;
    }

    @ActivityScope
    @Provides
    List<ClazzScoreData> provideLists() {
        return new ArrayList<>();
    }

    @ActivityScope
    @Provides
    @ScopeType()
    RecyclerView.LayoutManager provideLayoutManager() {
        return new GridLayoutManager(view.getActivity(), 3);
    }

    @ActivityScope
    @Provides
    @ScopeType(2)
    RecyclerView.LayoutManager provideLayoutDeleteManager() {
        return new LinearLayoutManager(view.getActivity());
    }

    @ActivityScope
    @Provides
    RecyclerView.ItemDecoration provideItemDecoration() {
        return new GridSpacingItemDecoration(3, RxUtil.getAutoHeight(view.getActivity(), 20), true);
    }

    @ActivityScope
    @Provides
    @ScopeType()
    BaseQuickAdapter<ClazzScoreData, AutoBaseViewHolder> providerAdapter(List<ClazzScoreData> lists) {
        return new BaseQuickAdapter<ClazzScoreData, AutoBaseViewHolder>(R.layout.clazz_review_item_score, lists) {
            @Override
            protected void convert(AutoBaseViewHolder helper, ClazzScoreData item) {
                if (!item.isEdit()) {
                    Glide.with(mContext)
                            .load(item.getIconUrl(mContext))
                            .apply(RequestOptions.placeholderOf(R.drawable.public_img_default).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL))
                            .into((ImageView) helper.getView(R.id.iv_icon));

                    if (item.getScoreType() == 1) {
                        helper.setText(R.id.btn_del, "+ " + item.getScore());
                    } else if (item.getScoreType() == 2) {
                        helper.setText(R.id.btn_del, "- " + item.getScore());
                    }
                    helper.setGone(R.id.btn_del, true);
                } else {
                    helper.setGone(R.id.btn_del, false);
                    Glide.with(mContext)
                            .load(item.getResIcon())
                            .into((ImageView) helper.getView(R.id.iv_icon));
                }
                helper.setText(R.id.tv_name, item.getTitle());
            }
        };

    }

    @ActivityScope
    @Provides
    @ScopeType(2)
    BaseQuickAdapter<ClazzScoreData, AutoBaseViewHolder> providerDeleteAdapter(List<ClazzScoreData> lists) {
        return new BaseQuickAdapter<ClazzScoreData, AutoBaseViewHolder>(R.layout.clazz_review_item_delete, lists) {
            @Override
            protected void convert(AutoBaseViewHolder helper, ClazzScoreData item) {
                Glide.with(mContext)
                        .load(item.getIconUrl(mContext))
                        .into((ImageView) helper.getView(R.id.iv_icon));
                helper.setText(R.id.tv_name, item.getTitle());
                helper.addOnClickListener(R.id.iv_delete);
            }
        };

    }

}